<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Article extends CI_Controller {
	
	private $tab = 'site';
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}
	
	function add_view(){
		$this->load->helper(array('form'));
		$this->load->model('Site_model');
  		$vdata['site_array'] = $this->Site_model->get_site_title_array();
  		
  		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'site_news';
		
		$this->load->view("/ychadmin/article_add", $vdata);
	}
	
	function add(){
		$nid 		= $this->input->post('nid');
		$title 		= $this->input->post('title');
		$ptime 		= $this->input->post('ptime');
		$author 	= $this->input->post('author');
		$fsource 	= $this->input->post('fsource');
		$summary 	= $this->input->post('summary');
		$forder 	= $this->input->post('forder');
		$content 	= $this->input->post('content');
		if($ptime != ''){
			$ptime = strtotime($ptime);
		}else{
			$ptime = time();
		}
		
		//如果有上传图片，则先完成图片上传
		if(!file_exists(UPFILES_ARTICLE_IMG)){
			mkdir(UPFILES_ARTICLE_IMG);
		}
		$thumbnail = "";
		if($_FILES['thumbnail']['name'] != '')
		{ 
			$thumbnail = sha1($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'].time().rand());
			$tmpfn = $_FILES['thumbnail']['name'];
			$pt=strrpos($tmpfn, ".");
			$retval=substr($tmpfn, $pt+1, strlen($tmpfn) - $pt);
			$thumbnail.=".".$retval;
			$upfile = UPFILES_ARTICLE_IMG."/".$thumbnail;
			if(is_uploaded_file($_FILES['thumbnail']['tmp_name']))
				move_uploaded_file($_FILES['thumbnail']['tmp_name'],$upfile);
		}
		
		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('title', 				'标题', 		'trim|min_length[1]|max_length[50]');	
		$this->form_validation->set_rules('forder', 			'排序', 		'trim|integer|min_length[1]|max_length[20]');
		$this->form_validation->set_rules('content', 			'内容', 		'required');
		if ($this->form_validation->run() == FALSE){
   			$this->load->view('/ychadmin/article_add');
   			return;
  		}
  		$data['nid'] 		= $nid;
  		$data['title'] 		= $title;
  		$data['ptime'] 		= $ptime;
  		$data['author'] 	= $author;
  		$data['thumbnail'] 	= $thumbnail;
  		$data['summary'] 	= $summary;
  		$data['forder'] 	= $forder;
  		$data['fsource'] 	= $fsource;
  		$data['content'] 	= $content;
  		$data['add_time'] 	= time();
  		$data['addip'] 		= $this->js->get_ip();
  		$this->load->model('article_model');
  		$article_id = $this->article_model->add($data);
  		
  		header("location:/ychadmin/article/articlelist/");
		$this->load->model('log_model');$this->log_model->add_log('[添加][文章]['.$title.']');
   		exit;
	}
	
	function view($article_id){
		$this->load->model('article_model');
  		$article = $this->article_model->get_article_obj($article_id);
  		$vdata['article'] = $article;
  		$this->load->view("/ychadmin/article_view", $vdata);
	}
	
	function edit_view($article_id){
		$this->load->helper(array('form'));
		$this->load->model('article_model');
  		$article = $this->article_model->get_article_obj($article_id);
  		$vdata['article'] = $article;
  		$this->load->model('Site_model');
  		$vdata['site_array'] = $this->Site_model->get_site_title_array();
  		
  		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'site_news';
		
  		$this->load->view("/ychadmin/article_edit", $vdata);
	}
	
	function edit(){
		$id 		= $this->input->post('id');
		$nid 		= $this->input->post('nid');
		$title 		= $this->input->post('title');
		$ptime 		= $this->input->post('ptime');
		$author 	= $this->input->post('author');
		$summary 	= $this->input->post('summary');
		$forder 	= $this->input->post('forder');
		$fsource 	= $this->input->post('fsource');
		$content 	= $this->input->post('content');
		
		$bthumbnail 		= $this->input->post('bthumbnail');
		$thumbnail = "";
		if($_FILES['thumbnail']['name'] != ''){ 
			$thumbnail = sha1($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'].time().rand());
			$tmpfn = $_FILES['thumbnail']['name'];
			$pt=strrpos($tmpfn, ".");
			$retval=substr($tmpfn, $pt+1, strlen($tmpfn) - $pt);
			$logoimg.=".".$retval;
			$upfile = UPFILES_ARTICLE_IMG."/".$thumbnail;
			if(is_uploaded_file($_FILES['thumbnail']['tmp_name']))
				move_uploaded_file($_FILES['thumbnail']['tmp_name'],$upfile);
		}
		$thumbnail = $thumbnail?$thumbnail:$bthumbnail;
		
		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('id', 				'ID', 		'required');
		$this->form_validation->set_rules('title', 				'标题', 		'trim|min_length[1]|max_length[50]');	
		$this->form_validation->set_rules('forder', 			'排序', 		'trim|integer|min_length[1]|max_length[20]');
		$this->form_validation->set_rules('content', 			'内容', 		'required');
		if ($this->form_validation->run() == FALSE){
   			$this->load->view('/ychadmin/article_edit');
   			return;
  		}
  		
  		$data['title'] 		= $title;
  		$data['nid'] 		= $nid;
  		$data['ptime'] 		= strtotime($ptime);
  		$data['author'] 	= $author;
  		$data['forder'] 	= $forder;
  		$data['thumbnail'] 	= $thumbnail;
  		$data['summary'] 	= $summary;
  		$data['fsource'] 	= $fsource;
  		$data['content'] 	= $content;
  		$this->load->model('article_model');
  		$article_id = $this->article_model->update($id, $data);
  		
  		header("location:/ychadmin/article/articlelist/");
		$this->load->model('log_model');$this->log_model->add_log('[修改][文章]'.'ID=='.$id );
   		exit;
	}
	
	function articlelist(){
		//接收参数
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$nid 				= $this->input->get('nid');
		$title 				= $this->input->get('title');
		
		$data = array();
		if($title != ''){
			$data['where']['title'] = $title;
		}
		
		if($nid != 0){
			$data['where']['nid'] = $nid;
		}
		
		$data['page_size']  = YTZ_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$data['select'] 	= 1;
		$this->load->model('article_model');
		$vdata = $this->article_model->get_new_article_list($data);
		
		$this->load->model('site_model');
		$sdata = $this->site_model->get_site_title_array();
		$vdata['site_array'] = $sdata;
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/article/articlelist/'.$nid.'?title='.$title;
		$config['total_rows'] = $vdata['article_count'];
		$config['per_page'] = YTZ_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['title'] 	= $title;
		$vdata['nid'] 		= $nid;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'site_news';
		
		$this->load->view('/ychadmin/article_list', $vdata);
	}
	
	function remove(){
		$id 				= $this->input->post('id');
  		$data["is_del"]		= 1;
  		$this->load->model('article_model');
		echo  $this->article_model->update($id, $data);
  		$this->load->model('log_model');$this->log_model->add_log('[删除][文章]');
	}
	
}
